﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ODAF.SilverlightApp.VO;

namespace ODAF.SilverlightApp
{
    public partial class RatingView : UserControl
    {
        public PointDataView ParentView { get; set; }

        private PointDataSummary _data;
        public PointDataSummary Data 
        {
            get { return _data; }

            set 
            {
                _data = value;
                UpdateView();
            }
        }

        public RatingView()
        {
            InitializeComponent();
        }

        public void Show()
        {
            this.Visibility = Visibility.Visible;
        }

        public void UpdateView()
        {
            if (Data.RatingCount > 0)
            {
                double ratingVal = Data.RatingTotal / Data.RatingCount * 5.0 / 100.0; // out of 5 stars, but stored from 0-100
                tbRatingText.Text = String.Format("{0} ({1} Ratings)", ratingVal.ToString(), Data.RatingCount);
                RectangleGeometry bounds = new RectangleGeometry();
                bounds.Rect = new Rect(0,0,16 * ratingVal,16);

                ActiveStarsPanel.Clip = bounds;
                ActiveStarsPanel.Visibility = Visibility.Visible;
            }
            else
            {
                ActiveStarsPanel.Visibility = Visibility.Collapsed;
                tbRatingText.Text = "No Ratings";
            }

            tbTitle.Text = Data.Name;
            if (Data.CurrentUserRating > 0)
            {
                tbCurrentUserRating.Text = string.Format("You rated this landmark {0} stars", (Data.CurrentUserRating / 20));
            }
            else
            {
                if (ParentView.User.IsAuthenticated)
                {
                    tbCurrentUserRating.Text = "Click a star to add your rating.";
                }
                else
                {
                    tbCurrentUserRating.Text = "You need to sign in to rate this landmark.";
                }
            }
        }

        private void btnStar_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (Data.CurrentUserRating == 0)
            {
                Image star = (Image)sender;
                int rating = 0;

                switch (star.Tag.ToString())
                {
                    case "1":
                        rating = 20; break;
                    case "2":
                        rating = 40; break;
                    case "3":
                        rating = 60; break;
                    case "4":
                        rating = 80; break;
                    case "5":
                        rating = 100; break;

                }

                ParentView.AddItemRating(this.Data.Guid, rating);
            }

        }

        private void OnCloseButton(object sender, MouseButtonEventArgs e)
        {
            ParentView.HideView();
        }
    }
}
